<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    table{
        border: 1 PX solid #e9e9e9;
        border-collapse: collapse;
        border-spacing: 0 ;
    }
    th,td{
        
        padding: 8px 16px;
        border: 1px solid #e9e9e9;
        text-align: left;
    } 
    th{
        background-color: #f7f7f7;
        color: #5c6b77;
        font-weight: 600;
    }
</style>
<body>
    <div id="app">
        <h2>{{message}}</h2>
        <div v-if="books.length">
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th>书籍名称</th>
                        <th>价格</th>
                        <th>数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in books">
                        <td>{{item.id}} </td>
                        <td>{{item.name}}</td>
                        <td>{{item.price | showPrice}}</td>
                        <td>
                            <button @click="sub(index)" v-bind:disabled="item.count <= 1" >-</button>
                            {{item.count}}
                            <button @click="add(index)">+</button></td>
                        <td><button @click="removrHandle(index)">删除</button></td>
                    </tr>
                </tbody>
            </table>
            <h2>总价格：{{totalPrice | showPrice}} </h2>
        </div>
        <h2 v-else=>购物车为空</h2>
    </div>
    <script src="vue/vue.js"></script>
    <script>
        const vm=new Vue({
            el:'#app',
            data:{
                message:'购物车',
                books:[
                    {
                        id:1,
                        name:'《物理》',
                        price:39.00,
                        count:1
                    },
                    {
                        id:2,
                        name:'《英语》',
                        price:49.00,
                        count:1
                    },
                    {
                        id:3,
                        name:'《语文》',
                        price:59.00,
                        count:1
                    },
                    {
                        id:4,
                        name:'《数学》',
                        price:69.00,
                        count:1
                    },
                ]
            },
            methods:{
                add(index){
                    this.books[index].count++
                },
                sub(index){
                    this.books[index].count--
                },
                removrHandle(index){
                    this.books.splice(index,1)
                }
            },
            computed:{
                totalPrice(){
                    let totalPrice = 0
                    for(let i = 0 ; i < this.books.length ; i++){
                        totalPrice += this.books[i].price * this.books[i].count
                    }
                    /* for(let i in this.books){
                        totalPrice += this.books[i].price * this.books[i].count
                    } */
                    /* for(let item of this.books){
                        totalPrice += item.price * item.count
                    } */
                    /* return this.books.reduce(function (param,books) {  
                        return param+books.price * books.count
                    },0) */
                    /* 函数式编程->链式编程 数组filter/map/reduce */
                    /* 返回值为true时返回，为false时过滤 */
                    /* 对每项操作 */
                    /* 对数组中的所有内容汇总 */
                    const nums = [15,29,35,54,98,64,15,2,70,4,6,7,52,67,46]
                    /* let nums1 = nums.filter(function(n){
                        return n>50
                    })
                    let nums2 = nums1.map(function (n) { 
                        return n*2
                    }) 
                    let totalPrice = nums2.reduce(function (preValue,n) { 
                        return preValue + n
                    },0) */
                    /* let totalPrice = nums.filter(function(n){
                        return n>50
                    }).map(function (n) { 
                        return n*2
                    }) .reduce(function (preValue,n) { 
                        return preValue + n
                    },0) */
                    //let totalPrice = nums.filter(n=>n>50).map(n=>n*2).reduce((preValue,n)=>preValue + n)
                    return totalPrice
                }
            },
            filters:{
                /* 过滤器 */
                showPrice(price){
                    return  '¥' + price.toFixed(2)
                }
            }

        });
    </script>
</body>
</html>